from time import strftime
import pygame
from pygame.locals import *

class _pallete:
    m_pal = None

# --------------------------------------------------------------------------------------     
    def __init__(self):
# -------------------------------------------------------------------------------------- 
        self.m_pal = {}

# -------------------------------------------------------------------------------------- 
# Returns a int in format of 0xRRGGBB as the color 'unique' ID       
    def add_color(self, R, G, B ):
# -------------------------------------------------------------------------------------- 
        newID = (R << 16) + (G << 8) + (B)
        if newID in self.m_pal :
            return newID
        
        self.m_pal[newID] = ((R, G, B))
        
        return newID

# --------------------------------------------------------------------------------------     
    def remove_color(self, colorID):
# -------------------------------------------------------------------------------------- 
        if colorID > -1 and colorID < len(self.m_List) :
            del self.m_pal[colorID]
            return len(self.m_pal)
        return None

# --------------------------------------------------------------------------------------     
    def use_color(self, colorID):
# -------------------------------------------------------------------------------------- 
        if colorID in self.m_pal :
            return self.m_pal[colorID]
        return None
    
class _gfx:


    m_Screen = None
    m_Buffer = None
    m_center = None
    m_Font_dict = None
    m_height = 0
    m_width = 0
    pallete = None
    m_Sprites = None
    m_Color_key = None 
    logger = None
    
   
# --------------------------------------------------------------------------------------     
    def __init__(self, screen, log):
# -------------------------------------------------------------------------------------- 
        self.logger = log
        self.logger.log.info("Initializing Graphics..")
        self.m_Screen = screen;
        self.m_height = screen.get_height()
        self.m_width = screen.get_width()
        self.m_center = (screen.get_size()[0]/2,screen.get_size()[1]/2)
        self.m_Buffer = pygame.Surface(self.m_Screen.get_size())
        self.m_Buffer = self.m_Buffer.convert()
        
        #initialize font list with the default font/size
        self.m_Font_dict = { 0:pygame.font.Font(None, 9) , }
        
        self.pallete = _pallete()
        
        self.m_Sprites = {}
        
        
        
# -------------------------------------------------------------------------------------- 
    def cls(self, color, loc = None, size = None):
# -------------------------------------------------------------------------------------- 
        area = None 
        if loc != None and size != None:
            area = pygame.Rect(loc, size)
            
        self.m_Buffer.fill(color, area)
        self.flip_buffers()
# --------------------------------------------------------------------------------------    
    def flip_buffers(self):
# -------------------------------------------------------------------------------------- 
        self.m_Screen.blit(self.m_Buffer, (0,0))
        pygame.display.flip()  

#Font Functions
# -------------------------------------------------------------------------------------- 
    def add_font(self,font_path, size = 9):
# -------------------------------------------------------------------------------------- 
        if font_path in self.m_Font_dict :
            return font_path
            
        self.m_Font_dict[font_path] = ( pygame.font.Font( font_path, size ) )
        return font_path

# -------------------------------------------------------------------------------------- 
    def rem_font(self, fontID):
# -------------------------------------------------------------------------------------- 
        if fontID in self.m_Font_dict and fontID != 0 :
            del self.m_Font_dict[fontID]
            return len(self.m_Font_list)
        return None
            
# --------------------------------------------------------------------------------------             
    def use_font(self, fontID):
# --------------------------------------------------------------------------------------     
        if fontID in self.m_Font_dict :
            return self.m_Font_dict[fontID]
        
# --------------------------------------------------------------------------------------         
    def write_text(self,loc, msg, aa, col,fontID = 0):
# --------------------------------------------------------------------------------------         
        if fontID in self.m_Font_dict :
            text_surface = self.m_Font_dict[fontID].render(msg, aa, col)
            self.m_Buffer.blit( text_surface, loc )
            return text_surface.get_size()
    
    def add_sprite(self, sp_path):
        
        self.m_Sprites[sp_path] = None
        try:
            image = pygame.image.load(sp_path)
        except pygame.error, message:
            image = None
        if image != None:
            image.convert()
            self.m_Sprites[sp_path] = image 
            if self.m_Color_key != None :
                self.m_Sprites[sp_path].set_colorkey(self.m_Color_key, RLEACCEL)
                
        return sp_path
    
    def remove_sprite(self, sp_path):
        
        del self.m_Sprites[sp_path]
    
    def draw_sprite(self, spriteID, x, y):
        
        if spriteID in self.m_Sprites :
                self.m_Buffer.blit( self.m_Sprites[spriteID], (x, y) )
            
            
#Draw Function Wrappers    
    def setp(self, loc, color):
        self.m_Buffer.set_at(loc, color)
# --------------------------------------------------------------------------------------
    def draw_line(self, start, end, color, width = 0):
# --------------------------------------------------------------------------------------
        pygame.draw.line( self.m_Buffer, color, start, end, width)
        
# --------------------------------------------------------------------------------------
    def draw_polygon(self, vertexl, color, width = 0):
# --------------------------------------------------------------------------------------  
        pygame.draw.polygon( self.m_Buffer, color, vertexl, width)

# --------------------------------------------------------------------------------------     
    def draw_rectangle(self, left, top, w, h, color, width = 0):
# --------------------------------------------------------------------------------------    
        pygame.draw.polygon( self.m_Buffer, color, ((left, top), (left, top+h),(left+w, top+h),(left+w,top)), width)
        
# --------------------------------------------------------------------------------------
    def draw_circle(self, pos, radius, color,width = 0):
# --------------------------------------------------------------------------------------
        pygame.draw.circle(self.m_Buffer, color, pos, radius, width)

# --------------------------------------------------------------------------------------    
    def draw_ellipse(self, area, color, width = 0):
# --------------------------------------------------------------------------------------
        pygame.draw.ellipse(self.m_Buffer, color, area, width)
        
# -------------------------------------------------------------------------------------- 
    def draw_arc(self, area, start_angle, end_angle, color, width = 1):
# --------------------------------------------------------------------------------------
        pygame.draw.arc(self.m_Buffer, color, area, start_angle, end_angle, width)
        
        
        
        
        